﻿using Note2.Db;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Note2._0
{
    public partial class NoteApp : Form
    {
        public NoteApp()
        {
            InitializeComponent();
            NoteApp_Load();
        }

        private void NoteApp_Load(string key = "")
        {
            // TODO: This line of code loads data into the form.
            // It was empty for now.    
            poisonListView1.Items.Clear();

            var list = SqlSugarHelper.Db.Queryable<NoteRecord>().WhereIF(!string.IsNullOrEmpty(key), m => m.ServerDesc.Contains(key) || m.ServerIP.Contains(key)).OrderByDescending(m => m.OrderSort).ToList();

            foreach (var dtl in list)
            {
                ListViewItem item = new ListViewItem
                {
                    Text = dtl.NoteType
                };
                item.SubItems.Add(dtl.ServerAccount);
                item.SubItems.Add(dtl.ServerPassWord);
                item.SubItems.Add(dtl.ServerIP);
                item.SubItems.Add(dtl.ServerDesc);
                poisonListView1.Items.Add(item);
            }
        }

        private void foxButton1_Click(object sender, EventArgs e)
        {
            this.textBoxEdit1.Text = string.Empty;
            var form = new NoteAdd();
            form.ShowDialog();
            NoteApp_Load();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (poisonListView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请选择一行的记录！");
                return;
            }

            string dungeon = poisonListView1.SelectedItems[0].SubItems[0].Text;

            string account = poisonListView1.SelectedItems[0].SubItems[1].Text;
            string passWord = poisonListView1.SelectedItems[0].SubItems[2].Text;
            string ip = poisonListView1.SelectedItems[0].SubItems[3].Text;
            string desc = poisonListView1.SelectedItems[0].SubItems[4].Text;
            var form = new NoteAdd(desc, ip, account, passWord, dungeon);
            form.ShowDialog();
            NoteApp_Load();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (poisonListView1.SelectedItems.Count > 0)
            {
                string account = poisonListView1.SelectedItems[0].SubItems[1].Text;
                string passWord = poisonListView1.SelectedItems[0].SubItems[2].Text;
                string ip = poisonListView1.SelectedItems[0].SubItems[3].Text;

                string cmdtext = string.Format("mstsc /v:{0} /w:{1} /h:{2}", ip, "1920", "1080");
                string entitySql = $"update RemoteLogging SET orderSort=orderSort+1 where ServerIP ='{ip}'";
                var entity = SqlSugarHelper.Db.Queryable<NoteRecord>().Where(m => m.ServerIP == ip).First();
                entity.OrderSort++;
                SqlSugarHelper.Update(entity);

                using (Process process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo("cmd.exe")
                    {
                        RedirectStandardInput = true,
                        UseShellExecute = false
                    };
                    process.Start();

                    process.StandardInput.WriteLine($"cmdkey /generic:TERMSRV/{ip} /user:{account} /pass:{passWord}");
                    process.StandardInput.WriteLine(cmdtext);
                    process.StandardInput.WriteLine("exit");
                    process.WaitForExit();
                }
            }
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (poisonListView1.SelectedItems.Count > 0)
            {
                string ip = poisonListView1.SelectedItems[0].SubItems[3].Text;
                if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    var entity = SqlSugarHelper.Db.Queryable<NoteRecord>().Where(m => m.ServerIP == ip).First();
                    SqlSugarHelper.Delete(entity);
                    NoteApp_Load();
                }
            }
        }

        private void textBoxEdit1_TextChanged(object sender, EventArgs e)
        {
            NoteApp_Load(this.textBoxEdit1.Text);
        }
    }

}
